# 文件操作
# open函数
# 语法 open(文件路径，模式，encoding=编码模式)   返回值 返回一个文件对象


'''
# 读取文件 mode为r read，write，append 一个文件对象，只能被读取一次
file = open("./files/test.txt","r",encoding="utf-8")

# 查看内容
# ----read方法
# ----readlines()  打印所有行 返回值 列表
#print(file.readines())
# ----readline() 一行一行读
print(file.readline())

# 循环读取
for line in file:
    print(line)

# 关闭文件夹
file.close()


# with open语法
with open("./files/test.txt","r",encoding="utf-8") as f:
    print(f.readlines())
'''



# 练习  python出现的次数
# 1.
with open('./files/1file.txt','r',encoding='utf-8') as f:
    # 将文件内容读取出来
    result = f.read()
    # 调用字符串count方法，传入参数
    print(result.count('Python'))
print('------------------------------')

# 2.
with open('./files/1file.txt','r',encoding='utf-8') as f:
    # 通过read方法读取文件内容并打印
    res = f.read()
    print(res)
    # 行数
    str_list = res.split('\n')
    # 行数累加
    count = 0
    for i in str_list:
        count += 1
    print(f"总行数为：{count}")
print('------------------------------')

# 3.
with open('./files/1file.txt','r',encoding='utf-8') as f:
    res = f.readlines()
    print(res)
    print(res.count('Python'))

